import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef, OnInit } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { AlterationInvoiceDto, AlterationInvoiceServiceProxy, AlterationInvoiceItemDto, TechFileInfoDto, TechFileInfoServiceProxy } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import { finalize } from 'rxjs/operators';
import { Table } from 'primeng/components/table/table';
import { EditAlterationComponent } from './edit-alteration-techfileinfo.component';

@Component({
  selector: 'AlterItem',
  templateUrl: './alterationinvoice-item.component.html'
})

export class CreateOrEditAlterItemComponent extends AppComponentBase implements OnInit {

  @ViewChild('createOrEditModal') modal: ModalDirective;
  @ViewChild('dt') dataTable: Table;
  @ViewChild('EditAlteration') EditAlteration: EditAlterationComponent;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
  isInit = true; //是否为初始，true，不查询成品
  active = false;
  saving = false;

  order: AlterationInvoiceDto = new AlterationInvoiceDto();
  orderitems: Array<AlterationInvoiceItemDto> = new Array<AlterationInvoiceItemDto>();

  selectedContact: AlterationInvoiceItemDto;

  constructor(
    injector: Injector,
    private _alterService: AlterationInvoiceServiceProxy,
    private _techServiceProxy: TechFileInfoServiceProxy
  ) {
    super(injector);
  }

  ngOnInit(): void {

  }

  show(id?: string): void {
    this.active = true;
    this.isInit = true;
    this.orderitems = new Array<AlterationInvoiceItemDto>();
    this._alterService.getAlterationForEdit(id).subscribe(result => {
      this.order = result;
      if ( result.alterationInvoiceItems ) {
          this.orderitems = result.alterationInvoiceItems;
      }
      this.modal.show();
    });
  }

  saveall(): void {
    this.saving = true;
    this.order.alterationInvoiceItems = this.orderitems;
    this._alterService.createOrUpdateAlteration(this.order)
      .pipe(finalize(() => { this.saving = false; }))
      .subscribe(() => {
        this.notify.info(this.l('SavedSuccessfully'));
        this.close();
        this.modalSave.emit(null);
      });
  }

  onRowSelect(event) {
    this.EditAlteration.show(this.selectedContact);
  }

  reload(orderitems?: Array<AlterationInvoiceItemDto>) {
    this.primengTableHelper.showLoadingIndicator();
    orderitems.forEach(element => {
      if (!this.orderitems.find(o => o.oldTechFileInfoId === element.oldTechFileInfo.id)) {
        this._techServiceProxy.getTechFileInfoForEdit(element.oldTechFileInfo.id).subscribe(result => {
          element.newTechFileInfo = result;
          //复制一个新的
          element.newTechFileInfo.id = undefined;
          this.orderitems.push(element);
        });
      }
    });
    this.primengTableHelper.hideLoadingIndicator();
  }

  delete(id: string, oldid: string) {
    this.message.confirm('确认移除该文件吗？', isConfirmed => {
      if (isConfirmed) {
        if (id) {
          this._alterService.deleteAlterationItem(id).subscribe(() => {
            this.notify.success(this.l('SuccessfullyDeleted'));
            this.orderitems = this.orderitems.filter(o => o.oldTechFileInfoId !== oldid);
          });
        } else {
          this.notify.success(this.l('SuccessfullyDeleted'));
          this.orderitems = this.orderitems.filter(o => o.oldTechFileInfoId !== oldid);
        }
      }
    });
  }


  close(): void {
    this.active = false;
    this.modal.hide();
  }
}
